package com.llmcu.msb.architect.concurrent.basic;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 线程状态
 *
 * @author liuling
 * @date 2024/9/17 11:37
 */
public class ThreadStatus {
    volatile static boolean workCommand = true;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        Thread boss = new Thread(() -> {
            System.out.println("boss start...");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            workCommand = false;
            System.out.println("boss end...");
        });
        Thread calculator = new Thread(() -> {
            System.out.println("calculator:start working...");
            Random random = new Random();
            double result = 0.0;
            while (workCommand) {
                result = 0.0;
                for (int i = 0; i < 1_0000_0000; i++) {
                    result += random.nextDouble();
                }
            }
            System.out.println(String.format("calculator: result={%s}...", result));
        });
        Thread cleaner = new Thread(() -> {
            System.out.println("cleaner:start waiting...");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("cleaner:start working...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("cleaner end...");
        });
        new Thread(new ToiletFisher()).start();
        calculator.start();
        cleaner.start();
        System.out.println("new完线程后，状态是：" + boss.getState());
        boss.start();
        System.out.println("线程start的一瞬间，状态是：" + boss.getState());
        Thread.sleep(1000);
        Thread toiletFish = new Thread(new ToiletFisher());
        toiletFish.start();
        System.out.println("线程sleep时，状态是：" + boss.getState());
        System.out.println("线程运行时，状态是：" + calculator.getState());
        System.out.println("线程等待时，状态是：" + cleaner.getState());
        Thread.sleep(1000);
        System.out.println("线程阻塞时，状态是：" + toiletFish.getState());
        boss.join();
        System.out.println("线程end完后，状态是：" + boss.getState());
        countDownLatch.countDown();
    }

    static class ToiletFisher implements Runnable {

        @Override
        public void run() {
            toiletFishing();
        }
    }

    static synchronized void toiletFishing() {
        System.out.println(String.format("摸鱼者{%s}开始摸鱼...", Thread.currentThread().getName()));
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(String.format("摸鱼者{%s}结束摸鱼...", Thread.currentThread().getName()));
    }
}
